<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html xmlns:th="http://www.thymeleaf.org">
<head>
  <title> ztree demo </title>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <link rel="stylesheet"  href="/layui/css/layui.css"/>
  <link rel="stylesheet" href="/ztree/css/zTreeStyle/zTreeStyle.css" type="text/css">
  <style>
    body {
      background-color: white;
      margin: 0;
      padding: 0;
      text-align: center;
    }

    div, p, table, th, td {
      list-style: none;
      margin: 0;
      padding: 0;
      color: #333;
      font-size: 12px;
      font-family: dotum, Verdana, Arial, Helvetica, AppleGothic, sans-serif;
    }
   .ztree li span.button.add {margin-left:2px; margin-right: -1px; background-position:-144px 0; vertical-align:top; *vertical-align:middle}
  </style>
  <script type="text/javascript" src="/ztree/js/jquery-1.4.4.min.js"></script>
  <script  src="/layui/layui.js"></script>
  <script src="/plugins/js/common.js"></script>
  <script type="text/javascript" src="/ztree/js/jquery.ztree.core.js"></script>
  <script type="text/javascript" src="/ztree/js/jquery.ztree.excheck.js"></script>
  <script type="text/javascript" src="/ztree/js/jquery.ztree.exedit.js"></script>
  <script type="text/javascript" th:inline="javascript">
  var layer;
  layui.use("layer",function(){
  	layer=layui.layer;
  });  
    var zTree;
    var setting = {
      view: {
			addHoverDom: addHoverDom,
			removeHoverDom: removeHoverDom,
			selectedMulti: false,
			dblClickExpand: false
	  },
	  async:{enable: true,url: getUrl},
      data: {
        simpleData: {
          enable: true,
          idKey: "id",
          pIdKey: "pId"
        }
      },
      callback: {
    	  beforeDrag: beforeDrag,
    	  onClick: zTreeOnClick,
    	  beforeEditName: beforeEditName,
    	  beforeRemove: beforeRemove
      },
	  edit:{
			enable:true,
			editNameSelectAll:true,
			removeTitle:"删除节点",
			renameTitle:"编辑节点",
			showRemoveBtn:showRemoveBtn,
			showRenameBtn:showRenameBtn
		}
	};
    
    function beforeDrag(treeId, treeNodes) {
		return false;
	}
    
    function beforeRemove(treeId, treeNode) {
    	layer.confirm('确认要删除该区域吗？', {
    		  btn: ['确认','取消'] //按钮
    		}, function(){
    		  var deleteResult = ajaxAction("/area/deleteAreaInfo",{code:treeNode.code,id:treeNode.id});
    		  layer.msg(deleteResult.returnMsg, {icon: 1});
    		  if(deleteResult.status==1){
    			  refresh(treeNode.pId);
    		  }
    		});
    	return false;
	}
    
    function showRemoveBtn(treeId, treeNode){
		return !isNull(treeNode.pId);
	}
	function showRenameBtn(treeId, treeNode){
		return !isNull(treeNode.pId);
	}
	
	function beforeEditName(treeId, treeNode) {
		return false;
	}
	
	function getUrl(treeId, treeNode) {
		return "/area/selectAreaInfoListByPid?pId="+treeNode.id;
	}
    
    function addHoverDom(treeId, treeNode) {
		var sObj = $("#" + treeNode.tId + "_span");
		if (treeNode.editNameFlag || $("#addBtn_"+treeNode.tId).length>0) return;
		var addStr = "<span class='button add' id='addBtn_" + treeNode.tId
			+ "' title='add node' onfocus='this.blur();'></span>";
		sObj.after(addStr);
		var btn = $("#addBtn_"+treeNode.tId);
		if (btn) btn.bind("click", function(){
			toAddAreaInfo(treeNode.id);
			return false;
		});
	};
	
	// 移除添加节点按钮
	function removeHoverDom(treeId, treeNode){
		$("#addBtn_"+treeNode.tId).unbind().remove();
	}
	//编辑
	function beforeEditName(treeId, treeNode) {
		toUpdateAreaInfo(treeNode.id);
		return false;
	}
    
    function zTreeOnClick(event, treeId, treeNode){
    	$("#initUserList").attr("src","/area/toUserListAreaInfo?areaId="+treeNode.id)
    }
    
    var zNodes = JSON.parse([[${areaListJson}]]);
    for(var j=0 ; j<zNodes.length; j++){//遍历目录节点，设置isParent属性为true;
  	  zNodes[j].isParent = true;
    }
    $(document).ready(function () {
      var t = $("#tree");
      zTree = $.fn.zTree.init(t, setting, zNodes);
    });

    function loadReady() {
      var bodyH = demoIframe.contents().find("body").get(0).scrollHeight,
        htmlH = demoIframe.contents().find("html").get(0).scrollHeight,
        maxH = Math.max(bodyH, htmlH), minH = Math.min(bodyH, htmlH),
        h = demoIframe.height() >= maxH ? minH : maxH;
      if (h < 530) h = 530;
      demoIframe.height(h);
    }
    
    function toAddAreaInfo(pId){
    	layer.open({
    		type:2,
	         title: "添加区域",
	         content: '/area/toAddAreaInfo?id='+pId,
	         area: ['700px', '90%'],
	         maxmin: true,            //最大化按钮
	         anim:3, //动画
	         shade: [0.8, '#393D49'],//遮罩层
    		}); 
    }
    
    function toUpdateAreaInfo(id){
    	layer.open({
    		type:2,
	         title: "修改区域",
	         content: '/area/toUpdateAreaInfo?id='+id,
	         area: ['700px', '90%'],
	         maxmin: true,            //最大化按钮
	         anim:3, //动画
	         shade: [0.8, '#393D49'],//遮罩层
    		}); 
    }
    
    function refresh(pId){
    	var node = zTree.getNodeByParam("id", pId, null);
    	zTree.reAsyncChildNodes(node, "refresh");
    }
    
  </script>
</head>

<body>
	<table border="0px;" width="100%">
		<tr>
			<td style="width:260px;">
				<div style="height: 35px;">
	      			<div class="grid-demo layui-bg-green" style="height: 35px;line-height: 40px;">请选择区域</div>
				</div>
				<ul id="tree" class="ztree" style="width:260px; overflow:auto;"></ul>
			</td>
			<td valign="top" align="left" rowspan="2" height="600px;" width="80%">
				<iframe vspace="0"  hspace="0" marginheight="0" marginwidth="0" height="500px;" id="initUserList" name="initUserList" width="100%" src="/area/toUserListAreaInfo" frameborder="0" scrolling="no"></iframe>
			</td>
		</tr>
		<tr>
		</tr>
	</table>
</html>
